package com.example.activity.implicitintent;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_second:
                // 根据Intent的Action启动Activity
                Intent intent = new Intent();
                intent.setAction("com.example.activity.SECOND_ACTIVITY");
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
                break;
            case R.id.btn_call:
                // 直接拨号
                // callPhone();
                // 显示拨号盘
                intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + "10086"));
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
                break;
            case R.id.btn_message:
                sendMessage();
                break;
            case R.id.btn_browser:
                openBrowser();
                break;
            case R.id.btn_map:
                openMap();
                break;
            case R.id.btn_alarm:
                createAlarm();
                break;
            case R.id.btn_intent:
                // Intent传递数据，以及Activity数据回传
                jumpSecond();
                break;
        }
    }

    private void sendMessage() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("smsto:10086"));
        intent.putExtra("sms_body", "10086, 你好");
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    private void openBrowser() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.baidu.com"));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    private void openMap() {
        Uri uri = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    // 闹钟需要设置权限
    private void createAlarm() {
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE, "起床")
                .putExtra(AlarmClock.EXTRA_HOUR, 6)
                .putExtra(AlarmClock.EXTRA_MINUTES, 0);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    /**
     * 直接打电话功能
     * 1. 在 AndroidManifest.xml设置权限
     * <uses-permission android:name="android.permission.CALL_PHONE" />
     * 2. 申请运行时权限（android 6.0以上版本）
     */
    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + "10086"));

        // 检查运行时权限，若没有则申请，然后在回调函数中完成打电话功能
        if (ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    1);
        } else {
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }
    }

    // 运行时权限申请的返回结果的处理
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            // 允许
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone();
            } else {
                // 拒绝
                Toast.makeText(MainActivity.this, "申请权限被拒绝", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void jumpSecond() {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);

        // 1. 通过Intent传递简单类型数据
        intent.putExtra("班级", "软件1811");

        // 2. 通过Bundle传递复杂类型：对象、对象集合
        Bundle bundle = new Bundle();
        bundle.putSerializable("user", new User("张三", "13801234567"));

        // 3. 通过Bundle传递对象集合
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("a", "13801234567"));
        users.add(new User("b", "13001234567"));
        users.add(new User("c", "13901234567"));
        bundle.putSerializable("users", users);

        intent.putExtras(bundle);
        startActivityForResult(intent, 101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 101 && resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    User user = (User) bundle.getSerializable("user");
                    if (user != null) {
                        Toast.makeText(MainActivity.this, "修改后的手机号：" + user.getPhone(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
}